package com.agileai.bpm.controller.resource;

import java.sql.Connection;
import java.sql.DriverManager;

import com.agileai.bpm.bizmoduler.resource.WfDbconfigManage;
import com.agileai.bpm.controller.define.WfDBOperatorEditHandler;
import com.agileai.common.AppConfig;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.common.BeanFactory;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.CryptionUtil;
import com.agileai.util.DBUtil;
import com.agileai.util.StringUtil;

public class WfDbconfigManageEditHandler extends StandardEditHandler{
	public WfDbconfigManageEditHandler(){
		super();
		this.listHandlerClass = WfDbconfigManageListHandler.class;
		this.serviceId = buildServiceId(WfDbconfigManage.class);
	}

	protected void processPageAttributes(DataParam param) {
		String password = this.getAttributeValue("WFD_PWD");
    	if (!StringUtil.isNullOrEmpty(password)){
    		AppConfig appConfig = BeanFactory.instance().getAppConfig();
    		String secretKey = appConfig.getConfig("GlobalConfig", "SecurityKey");
    		try {
        		String decPassword = CryptionUtil.decryption(password, secretKey);
        		this.setAttribute("WFD_PWD",decPassword);				
			} catch (Throwable e) {
				e.printStackTrace();
			}
    	}
	}
	public ViewRenderer doSaveAction(DataParam param){
		String operateType = param.get(OperaType.KEY);
		AppConfig appConfig = BeanFactory.instance().getAppConfig();
		String securityKey = appConfig.getConfig("GlobalConfig", "SecurityKey");
		String pwdValue = param.stringValue("WFD_PWD");
		String enscriptionPwdValue = CryptionUtil.encryption(pwdValue,securityKey);
		param.put("WFD_PWD", enscriptionPwdValue);
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);
			String comeFrom = param.get("comeFrom");
			if (!StringUtil.isNullOrEmpty(comeFrom)){
				String wfdId = param.get("WFD_ID");
				return new DispatchRenderer(getHandlerURL(WfDBOperatorEditHandler.class)
						+"&WFD_ID="+wfdId);	
			}
		}
		else if (OperaType.UPDATE.equals(operateType)){
			getService().updateRecord(param);	
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	protected WfDbconfigManage getService() {
		return (WfDbconfigManage)this.lookupService(this.getServiceId());
	}
	public ViewRenderer doBackAction(DataParam param){
		String comeFrom = param.get("comeFrom");
		if (StringUtil.isNullOrEmpty(comeFrom)){
			return new RedirectRenderer(getHandlerURL(listHandlerClass));			
		}else{
			return new DispatchRenderer(getHandlerURL(WfDBOperatorEditHandler.class));
		}
	}
    @PageAction
    public ViewRenderer testConnection(DataParam param){
    	String responseText = FAIL;
    	String driverClass = param.get("WFD_DRIVER");
    	String driverUrl = param.get("WFD_URL");
    	String userId = param.get("WFD_USER");
    	String userPwd = param.get("WFD_PWD");
    	if (this.isValidConnection(driverClass, driverUrl, userId, userPwd)){
    		responseText = SUCCESS;
    	}
    	return new AjaxRenderer(responseText);
    }
    private boolean isValidConnection(String driverClass,String driverUrl,String userId,String userPwd){
    	boolean result = false;
    	Connection connection = null;
    	try {
        	Class.forName(driverClass);
        	connection = DriverManager.getConnection(driverUrl,userId,userPwd);
        	result = true;
		} catch (Exception e) {
			result = false;
		} finally{
			DBUtil.close(connection);
		}
    	return result;
    }
}